#include <unistd.h>
#include <pthread.h>
#include <iostream>
#include "Wrappers/Pthread/Mutex.hpp"
#include "Wrappers/Pthread/MutexScopedLock.hpp"
#include "Wrappers/Pthread/ConditionVariable.hpp"
#include "Wrappers/Pthread/Tests/ConditionVariable_tests.hpp"

namespace wrappers {
namespace pthread {

ConditionVariable_tests::ConditionVariable_tests()
    : mTestRunner("Condition variables' tests")
{
  mTestRunner.AddTestCase(
      UTESTS_MAKE_CASE_DATA(ConditionVariable_tests::MainTest));
  mTestRunner.Run(true);
}

ConditionVariable_tests::~ConditionVariable_tests() {

}

UTESTS_START_TEST(ConditionVariable_tests::MainTest) {
  ConditionVariable conditionVariable;
  Mutex mutex;
  pthread_t thread;

  pthread_create(&thread, NULL, ConditionVariable_tests::ThreadTimedLocker,
      &conditionVariable);
  conditionVariable.Wait(mutex);
  pthread_join(thread, NULL);
}
UTESTS_END_TEST

void *ConditionVariable_tests::ThreadTimedLocker(void *args) {
  ConditionVariable *conditionVariable = (ConditionVariable *) args;

  usleep(1000);
  conditionVariable->NotifyAll();
  return NULL;
}

} /* namespace pthread */
} /* namespace wrappers */
